package com.zysdev.zysplayer.player

import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import com.zysdev.zysplayer.opengl.views.PlayerView
import com.zysdev.zysplayer.player.listener.OnPlayerListener

class ZYSPlayer {

    private var source: String = ""
    private var playerView: PlayerView? = null
    var onPlayerListener: OnPlayerListener? = null
    private var isPrepared = false
    private var totalTime = 0
    private var currentTime = 0
    private var isLoading = true
    private var handler: Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                1 -> {
                    onPlayerListener?.onPrepared(totalTime)
                }
                2 -> {
                    onPlayerListener?.onLoading(isLoading)
                }
                3 -> {
                    onPlayerListener?.onCurrentTime(currentTime, totalTime)
                }
            }
        }
    }

    init {
        System.loadLibrary("native-lib")
    }


    fun setSource(source: String) {
        if (source.isEmpty()) {
            return
        }
        this.source = source
    }

    /**
     * 播放器准备
     */
    fun prepare() {
        if (source.isEmpty()) {
            return
        }
        prepareNative(this.source)
    }

    /**
     * 播放，只调用一次
     * 准备完状态才可以播放
     */
    fun play() {
        if (source.isEmpty()) {
            return
        }
        if (!isPrepared) {
            return
        }
//        Thread {
            playNative()
//        }.start()
    }

    fun seekTo(parent: Int) {

        if (source.isEmpty()) {
            return
        }
        seekNative(parent)
    }

    fun setVolume(percent: Int) {
        if (percent in 0..100) {
            volumeNative(percent)
        }
    }

    /**
     * 暂停
     */
    fun pause() {
        pauseNative()
    }

    /**
     * 恢复播放
     */
    fun resume() {
        resumeNative()
    }

    /**
     * 设置声道
     * @param mute 0：right，1:left,2:立体center
     */
    fun setMute(mute: Int) {
        muteNative(mute)
    }

    /**
     * 设置倍速
     * @param speed 0.1f~2.0f
     */
    fun setSpeed(speed: Float) {
        speedNative(speed)
    }

    /**
     * 设置音调
     * @param pitch 0.1f~2.0f
     */
    fun setPitch(pitch: Float) {
        pitchNative(pitch)
    }

    /**
     * 停止，销毁
     */
    fun stop() {
        Thread {
            releaseNative()
        }.start()
    }

    /**
     * Native准备完成回调
     */
    private fun onCallPrepared(total: Int) {
        isPrepared = true
        totalTime = total
        handler.sendEmptyMessage(1)
    }

    /**
     * Native播放时间的回调
     */
    private fun onCallTimeInfo(current: Int, total: Int) {
        currentTime = current
        totalTime = total
        handler.sendEmptyMessage(3)
    }

    /**
     * Native播放时间的回调
     */
    private fun onCallLoad(load: Boolean) {
        isLoading = load
        handler.sendEmptyMessage(2)
    }

    private fun onCallRenderYUV(
        width: Int,
        height: Int,
        y: ByteArray?,
        u: ByteArray?,
        v: ByteArray?
    ) {
        if (playerView != null) {
            playerView!!.setYUVData(width, height, y, u, v)
        }
    }


    fun setPlayerView(playerView: PlayerView) {
        this.playerView = playerView
    }

    private external fun prepareNative(source: String)

    private external fun playNative()

    private external fun pauseNative()

    private external fun resumeNative()

    private external fun muteNative(mute: Int)

    private external fun seekNative(parent: Int)

    private external fun volumeNative(parent: Int)

    private external fun speedNative(speed: Float)

    private external fun pitchNative(pitch: Float)

    private external fun releaseNative()


}